package de.tsajar.es.networking;

import com.simsilica.es.Entity;
import com.simsilica.es.EntityComponent;
import com.simsilica.es.EntityId;
import java.util.Arrays;

/**
 * Name says all ;)
 *
 * @author Eike Foede
 */
public class ReadOnlyEntity implements Entity {

    private EntityId id;
    private EntityComponent[] components;
    private long lastUpdated;

    public ReadOnlyEntity(EntityId id, EntityComponent[] components) {
        this.id = id;
        this.components = components;
    }

    public void setLastUpdated(long lastUpdate) {
        this.lastUpdated = lastUpdate;
    }

    public long getLastUpdated() {
        return lastUpdated;
    }

    @Override
    public EntityId getId() {
        return id;
    }

    @Override
    public <T extends EntityComponent> T get(Class<T> type) {
        for (EntityComponent c : components) {
            if (c != null && c.getClass() == type) {
                return type.cast(c);
            }
        }
        return null;
    }

    @Override
    public void set(EntityComponent c) {
        throw new UnsupportedOperationException("Readonly entity!");
    }

    @Override
    public boolean isComplete() {
        for (int i = 0; i < components.length; i++) {
            if (components[i] == null) {
                return false;
            }
        }
        return true;
    }

    @Override
    public EntityComponent[] getComponents() {
        return components;
    }

    @Override
    public String toString() {
        return "Entity[" + id + ", values=" + Arrays.asList(components) + "]";
    }
}
